package com.alipay.sdk.abcphaima;

import android.os.Bundle;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.iot.bpaas.api.service.BPaaSCallback;
import com.alipay.iot.bpaas.api.service.BPaaSResponse;
import com.alipay.sdk.abcphaima.bean.AbcpPageResultData;
import com.alipay.sdk.util.LogUtil;
import com.alipay.sdk.util.TextUtil;

/**
 * @ProjectName: 团餐
 * @ClassName: AbcpHMResultPage
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 2022/8/15 5:33 下午
 */
public class AbcpHMResultPage {

    public static void showPayResult(AbcpPageResultData data, int type) {
        goScanCodeOfflinePayResult(data, type);
    }

    private static void goScanCodeOfflinePayResult(AbcpPageResultData data, int type) {
        String operateType = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", data.success);            //false为支付失败
        jsonObject.put("tradeNo", data.tradeNo);
        jsonObject.put("sessionId", data.sessionId);
        jsonObject.put("resultTimeOut", data.resultTimeOut);
        if (type == 0) {
            jsonObject.put("ftoken", data.authCode);    // 刷脸
            operateType = "showScanFaceOfflinePayResult";
        } else if (type == 1) {
            jsonObject.put("scanCode", data.authCode);  // 扫码
            operateType = "showScanCodeOfflinePayResult";
        } else if (type == 2) {
            operateType = "showScanCardOfflinePayResult";
        }

        jsonObject.put("advancedConfig", createAdvancedConfig(data).toJSONString()); // 必填，自定义结果页展示数据

        AbcpHM.startCanteenBPaaSService("payResult", operateType,
                jsonObject, new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse bPaaSResponse) {

                    }

                    @Override
                    public void onEvent(String s, String s1, Bundle bundle) {

                    }
                });
    }

    private static JSONObject createAdvancedConfig(AbcpPageResultData data) {
        JSONObject advancedConfig = new JSONObject();
        advancedConfig.put("version", "2.0");// 表示启用新版本结果页，必须

        JSONObject OuterSummary = new JSONObject();
        OuterSummary.put("actualAmount", TextUtil.formatPriceNoSymbol(data.paidAmount));// 总金额，非必须
        OuterSummary.put("enablePaymentVoice", true);// 语音播报开关，非必须
        OuterSummary.put("title", data.title);// 结果页标题，必须
        if (!data.success)
            OuterSummary.put("subTitle", data.subMsg);// 结果页子标题，非必须
        advancedConfig.put("OuterSummary", OuterSummary);

        if (data.success) {
//            JSONArray extend = new JSONArray();
//            JSONObject object = new JSONObject();
//            object.put("title", "");
//            object.put("color", "#333333");
//
//            JSONObject detail = new JSONObject();
//            detail.put("name", "应付金额");
//            detail.put("value", TextUtil.formatPrice(data.totalAmount));
//            object.put("detail", detail);
//            extend.add(object);
//
//            object = new JSONObject();
//            object.put("title", "");
//            object.put("color", "#ff3b30");
//
//            detail = new JSONObject();
//            detail.put("name", "补贴金额");
//            detail.put("value", "- " + TextUtil.formatPrice(data.discountAmount));
//            object.put("detail", detail);
//            extend.add(object);

            advancedConfig.put("OuterExtends", data.outerExtends);
        }
        return advancedConfig;
    }

    public static void closePayResult(String sessionId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sessionId", sessionId);
        AbcpHM.startCanteenBPaaSService("payResult", "close",
                jsonObject, new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse response) {
                        LogUtil.d("退出结果页 出参" + response);
                    }

                    @Override
                    public void onEvent(String eventId, String data, Bundle extra) {
                        LogUtil.d("退出结果页 onEvent"
                                + "\neventId = " + eventId
                                + "\ndata = " + data
                                + "\nextra = " + extra);
                    }
                });
    }
}
